$(function() {
	// 页面控件
	var $dataTable = $('#dataTable'); // 表格
	var $dataTableToolbar = $('#dataTableToolbar'); // 表格：工具栏
	var $cachedTime = $('#cachedTime');
	
	initWidgets();
	initListeners();
	
	$('#btnExport').on('click', function() {
		alert("逗你玩");
	});
	
	function refreshCachedTime() {
		$.ajax({
			url: '../time',
			type: 'GET',
			dataType: 'json',
			contentType: "application/json; charset=utf-8",
			success: function(result) {
				if (result.success) {
					$cachedTime.text(result.data);
				}
			}
		});
	}
	
	function getHeight() {
	    return $(window).height() - 40;
	}
	
	// 初始化事件监听
	function initListeners() {
		$(window).resize(function () {
			$dataTable.bootstrapTable('resetView', {
	            height: getHeight()
	        });
	    });
	}
	
	function initWidgets() {
		buildMinerTable();
	}
	
	function buildMinerTable() {
		var urlStr ="";
		if($('#shelfId').val()!=0)
			urlStr = '../../detection/minerFailures/'+$('#shelfId').val();
		else
			urlStr = '../../detection/minerFailures/-1';
		//alert(urlStr);
		//alert("jquery:"+$('#shelfId').val());
		//alert("js:"+document.getElementById("shelfId").value);
		$dataTable.bootstrapTable({
			height: getHeight(),
			url: urlStr,
			pagination: true,
			sidePagination: 'server',
			pageSize: 100,
			pageList: [10, 25, 50, 100],
			showRefresh: true,
			toolbar: '#dataTableToolbar',
			iconSize: 'outline',
			icons: {
				paginationSwitchDown: 'glyphicon-collapse-down icon-chevron-down',
	            paginationSwitchUp: 'glyphicon-collapse-up icon-chevron-up',
	            refresh: 'glyphicon-repeat',
				toggle: 'glyphicon-list-alt',
				columns: 'glyphicon-list',
	            detailOpen: 'glyphicon-plus icon-plus',
	            detailClose: 'glyphicon-minus icon-minus'
			},
			onLoadSuccess: function(data) {
				refreshCachedTime();
			},
			columns: [
			    [
					{
						field: 'shelfId',
						title: '机架',
						halign: 'center',
						align: 'left',
						valign: 'middle',
						rowspan: 2
					},
					{
						field: 'host',
						title: 'IP地址',
						halign: 'center',
						align: 'left',
						valign: 'middle',
						rowspan: 2
					},
					{
						field: 'pingable',
						title: '网络情况',
						align: 'center',
						valign: 'middle',
						rowspan: 2,
						formatter: function(value, row, index) {
							if (value == true) {
								return '<img src="../../img/icon/icon-check.png" width="20" height="20" />';
							} else if (value == false) {
								return '<img src="../../img/icon/icon-cross.png" width="20" height="20" />';
							} else {
								return value;
							}
						}
					},
					{
						field: 'mac',
						title: 'MAC地址',
						align: 'center',
						valign: 'middle',
						rowspan: 2,
						formatter: function(value, row, index) {
							if (value) {
								return value;
							} else {
								return '<img src="../../img/icon/icon-cross.png" width="20" height="20" />';
							}
						}
					},
					{
						field: 'runtime',
						title: '运行时状态',
						align: 'center',
						valign: 'middle',
						rowspan: 2,
						formatter: function(value, row, index) {
							var and = row.statsable & row.poolsable & row.summaryable;
							var or = row.statsable | row.poolsable | row.summaryable;
							if (and) {
								return '<img src="../../img/icon/icon-check.png" width="20" height="20" />';
							} else if (or) {
								return '<img src="../../img/icon/icon-check.png" width="20" height="20" />';
							} else {
								return '<img src="../../img/icon/icon-cross.png" width="20" height="20" />';
							}
						}
					},
					{
						field: 'minerType',
						title: '是否获取pools信息',
						align: 'center',
						valign: 'middle',
						rowspan: 2
					},
					{
						field: 'firmwareVersion',
						title: '是否获取summary信息',
						align: 'center',
						valign: 'middle',
						rowspan: 2
					},
					{
						field: 'pools',
						title: '矿池信息',
						align: 'center',
						colspan: 3
					}
			    ],
			    [
			     	{
			     		field: 'pool1',
			     		title: '1',
			     		halign: 'center',
						align: 'left',
						formatter: function(value, row, index) {
							var pools = row.pools;
							if (pools && pools.length >= 1) {
								var pool = pools[0];
								var url = pool.url ? pool.url : '-';
								var worker = pool.user ? pool.user : '-';
								return url + '<br>' + worker;
							}
						}
			     	},
			     	{
			     		field: 'pool2',
			     		title: '2',
			     		halign: 'center',
						align: 'left',
						formatter: function(value, row, index) {
							var pools = row.pools;
							if (pools && pools.length >= 2) {
								var pool = pools[1];
								var url = pool.url ? pool.url : '-';
								var worker = pool.user ? pool.user : '-';
								return url + '<br>' + worker;
							}
						}
			     	},
			     	{
			     		field: 'pool3',
			     		title: '3',
			     		halign: 'center',
						align: 'left',
						formatter: function(value, row, index) {
							var pools = row.pools;
							if (pools && pools.length >= 3) {
								var pool = pools[2];
								var url = pool.url ? pool.url : '-';
								var worker = pool.user ? pool.user : '-';
								return url + '<br>' + worker;
							}
						}
			     	}
			    ]
			]
		});
	}
	
	
});